package com.paic.mhis.hcpms.agreement.healthcareprovider.web;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.paic.mhis.hcpms.agreement.healthcareprovider.biz.service.AttendantProviderService;
import com.paic.pafa.appclient.annotation.BizServicesProxy;

@Controller
public class DownLoadPersonInfo {

	private Log log = LogFactory.getLog(this.getClass());

	@BizServicesProxy
	private AttendantProviderService attendantProviderService;

	private static List<String> insuranceHeaderList = new ArrayList<String>();
	static {

		insuranceHeaderList.add("姓名");
		insuranceHeaderList.add("机构名称");
		insuranceHeaderList.add("机构编号");
		insuranceHeaderList.add("性别");
		insuranceHeaderList.add("出生日期");
		insuranceHeaderList.add("人员分类");
		insuranceHeaderList.add("证件类型");
		insuranceHeaderList.add("证件号");
		insuranceHeaderList.add("保险号");
		insuranceHeaderList.add("医院科室");
		insuranceHeaderList.add("省科室");
		insuranceHeaderList.add("固定电话");
		insuranceHeaderList.add("手机号码");
		insuranceHeaderList.add("资格名称");
		insuranceHeaderList.add("有效期限");
		insuranceHeaderList.add("培训证号");
		insuranceHeaderList.add("是否有效");
		insuranceHeaderList.add("医保服务状态");
		insuranceHeaderList.add("医生证号");
		insuranceHeaderList.add("执业范围");
		insuranceHeaderList.add("执业证号");
		insuranceHeaderList.add("执业类别");
		insuranceHeaderList.add("资格级别");
		insuranceHeaderList.add("授予时间");
		insuranceHeaderList.add("学历");
		insuranceHeaderList.add("毕业学校");
		insuranceHeaderList.add("专业");
		insuranceHeaderList.add("类别");
		insuranceHeaderList.add("机构类别");
		insuranceHeaderList.add("机构等级");
		insuranceHeaderList.add("地址");

	}

	/**
	 * 导出医保服务人员信息
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/hcpms/provider/exportInsurance")
	@ResponseBody
	public void exportRules(HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> params = new HashMap<String, Object>();
		try {
			String healthProviderName = request
					.getParameter("healthProviderName");
			String hospitalLevel = request.getParameter("hospitalLevel");
			String managementCode = request.getParameter("managementCode");
			String personType = request.getParameter("personType");
			String qualifiedLevel = request.getParameter("qualifiedLevel");
			String certificateNo = request.getParameter("certificateNo");
			String name = request.getParameter("name");
			String practiceLicense = request.getParameter("practiceLicense");

			params.put("healthProviderName", healthProviderName);
			params.put("hospitalLevel", hospitalLevel);
			params.put("managementCode", managementCode);
			params.put("personType", personType);
			params.put("qualifiedLevel", qualifiedLevel);
			params.put("certificateNo", certificateNo);
			params.put("name", name);
			params.put("practiceLicense", practiceLicense);
			int total = this.attendantProviderService.queryContactCount(params);
			int limit = 8000;
			List insuranceList = new ArrayList<Map<String, Object>>();
			for (int i = 0; i < total;) {
				List resultListTemp = (List) this.attendantProviderService
						.getInsuranceList(i, limit, params);
				i += limit;
				insuranceList.addAll(resultListTemp);
			}
			String excelName = "医保服务人员信息";

			exportExcel(insuranceList, excelName, insuranceHeaderList, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void exportExcel(List resultList, String excelName,
			List<String> insuranceHeaderList, HttpServletResponse response)
			throws IOException {

		Workbook excel = null;
		try {
			excel = new HSSFWorkbook();
			Sheet sheet = excel.createSheet(excelName);
			Row row = sheet.createRow(0);
			// 设置Excel头
			int j = 0;
			for (String key : insuranceHeaderList) {
				row.createCell(j).setCellValue(key);
				sheet.setColumnWidth(j, 4000);
				j++;
			}
			// 设置Excel内容
			if (null != resultList) {
				for (int i = 0; i < resultList.size(); i++) {
					Map dto = (Map) resultList.get(i);
					row = sheet.createRow(i + 1);
					row.createCell(0).setCellValue((String) dto.get("name"));
					row.createCell(1).setCellValue(
							(String) dto.get("healthProviderName"));
					row.createCell(2).setCellValue(
							((String) dto.get("healthProviderCode")));
					row.createCell(3).setCellValue(((String) dto.get("Sex")));
					row.createCell(4).setCellValue(
							((String) dto.get("birthDate")));
					row.createCell(5).setCellValue(
							((String) dto.get("personType")));
					row.createCell(6).setCellValue(
							((String) dto.get("certificateType")));
					row.createCell(7).setCellValue(
							((String) dto.get("certificateNo")));
					row.createCell(8).setCellValue(
							(String) dto.get("insurancenum"));
					row.createCell(9)
							.setCellValue((String) dto.get("departNo"));
					row.createCell(10).setCellValue(
							(String) dto.get("departName"));
					row.createCell(11).setCellValue(
							(String) dto.get("contactPhone"));
					row.createCell(12).setCellValue(
							(String) dto.get("mobilePhone"));
					row.createCell(13).setCellValue(
							(String) dto.get("qualifiedName"));
					row.createCell(14).setCellValue((String) dto.get("yxqx"));
					row.createCell(15).setCellValue(
							(String) dto.get("trainingNo"));
					row.createCell(16)
							.setCellValue((String) dto.get("isValid"));
					row.createCell(17).setCellValue(
							(String) dto.get("serviceStus"));
					row.createCell(18).setCellValue(
							(String) dto.get("DOCTOR_LICENSE"));
					row.createCell(19).setCellValue(
							(String) dto.get("PRACTICE_SCOPE"));
					row.createCell(20).setCellValue(
							(String) dto.get("PRACTICE_LICENSE"));
					row.createCell(21).setCellValue(
							(String) dto.get("PRACTICE_TYPE"));
					row.createCell(22).setCellValue(
							(String) dto.get("QUALIFIED_LEVEL"));
					row.createCell(23).setCellValue(
							(String) dto.get("QUALIFIED_DATE"));
					row.createCell(24).setCellValue(
							(String) dto.get("EDUCATION"));
					row.createCell(25).setCellValue((String) dto.get("SCHOOL"));
					row.createCell(26).setCellValue(
							(String) dto.get("PROFESSIONAL"));
					row.createCell(27).setCellValue(
							(String) dto.get("CATEGORY"));
					row.createCell(28).setCellValue(
							(String) dto.get("ORGANIZATION_TYPE"));
					row.createCell(29).setCellValue(
							(String) dto.get("HOSPITAL_LEVEL"));
					row.createCell(30).setCellValue(
							(String) dto.get("FULL_ADDRESS"));

				}
			}

			initResponse(response, excelName);
			excel.write(response.getOutputStream());
		} catch (IOException e) {
			log.error("导出Excel是发生异常,异常原因:" + e.getMessage());
			e.printStackTrace();
		} finally {
			response.getOutputStream().flush();
			response.getOutputStream().close();

		}
	}

	private void initResponse(HttpServletResponse response, String name) {
		try {
			response.addHeader("Pragma", "no-cache");
			response.addHeader("Cache-Control", "no-cache");
			response.reset();
			response.setContentType("APPLICATION/OCTET-STREAM");
			String fileName = new String((name).getBytes("utf-8"), "ISO8859-1")
					+ ".xls";
			response.setHeader("content-disposition", "attachment;fileName=\""
					+ fileName + "\"");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}

}
